package org.example.service;


import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.example.mapper.OrderMapper;
import org.example.model.OrderInfo;
import org.example.model.ProductInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
@Slf4j
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DiscoveryClient discoveryClient;
    //计数器
    //在启动的时候，先启动product-service服务
    //否则要不然实例数为零
    private  AtomicInteger count=new AtomicInteger(1);
    private List<ServiceInstance> instances;
    @PostConstruct
    public void  init() {
        instances=discoveryClient.getInstances("product-service");
    }
    //这里需要搞负载均衡
    //换一种负载均衡
    public OrderInfo selectOrderById(Integer orderId){
        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
        String url="http://product-service/product/"+orderInfo.getProductId();
        log.info("远程调用:"+url);
        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
        orderInfo.setProductInfo(productInfo);
        return orderInfo;
    }



//    public OrderInfo selectOrderById(Integer orderId){
//        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//        int index=count.getAndIncrement()%instances.size();
//        String uri=instances.get(index).getUri().toString();
//        String url=uri+"/product/"+orderInfo.getProductId();
//        log.info("远程调用:"+url);
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }


//    @Autowired
//    private RestTemplate restTemplate;
//    public OrderInfo selectOrderById(Integer orderId){
//        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//        //远程调用
//        String url="http://127.0.0.1:9090/product/"+orderInfo.getProductId();
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }
//
//    public OrderInfo selectOrderById(Integer orderId){
//        OrderInfo orderInfo = orderMapper.selectOrderById(orderId);
//        //远程调用,有注册中心之后，就不需要将地址写死
//        List<ServiceInstance> instances = discoveryClient.getInstances("product-service");
//        String uri=instances.get(0).getUri().toString();
//        String url=uri+"/product/"+orderInfo.getProductId();
//        log.info("远程调用:"+url);
//        ProductInfo productInfo = restTemplate.getForObject(url, ProductInfo.class);
//        orderInfo.setProductInfo(productInfo);
//        return orderInfo;
//    }
}
